import {baseUrl} from './env'
import Vue from 'vue'
import {getStore} from "./mUtils";

export default (url = '', data = {}, type = 'GET') => {
  type = type.toUpperCase();
  url = baseUrl + url;
  let vm = new Vue()
  $.ajaxSetup({
    beforeSend: function (HttpRequest) {
      vm.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0)'
      });
    },
    complete: function () {
      vm.$loading().close()
    },
    error: function () {
      vm.$loading().close()
    }
  })
  try {
    const response = $.ajax({
      type: type,
      url: url, //'http://192.168.1.9:8080/ajeasy/user/api/login'
      async: false,
      dataType: 'json',
      data: data,
      timeout: 10000,
      header: {
        withCredentials: true,
        type:'admin'
      },
      xhrFields: {
        withCredentials: true
      },
      error: function(jqXHR, textStatus, errorThrown){
        switch (jqXHR.status){
          case(500):
            alert("服务器系统内部错误");
            break;
          case(408):
            alert("请求超时");
            break;
          case(0):
            alert("服务器系统内部错误");
            break;
          default:
            alert("未知错误");
        }
      },
    });
    return response
  } catch (error) {
    throw new Error(error)
  }

}
